import { LOGIN_TOKEN } from "@/global/constants";
import { localCache } from "@/utils/cache";
// import { firstMenu } from "@/utils/map-menus";
import { createRouter, createWebHashHistory } from "vue-router";

const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: "/",
      redirect: "main",
    },
    {
      path: "/login",
      name: "login",
      component: () => import("@/views/login/Login.vue"),
    },
    {
      path: "/main",
      name: "main",
      component: () => import("@/views/main/main.vue"),
    },
    {
      path: "/:pathMatch(.*)*",
      name: "notFound",
      component: () => import("@/views/not-found/NotFound.vue"),
    }
  ]
})

// // 1. 获取所有路由
// const localRoutes = [
//   // {
//   //   path: "/main/analysis/overview",
//   //   component: () => import("@/views/main/analysis/overview/Overview.vue")
//   // },
//   // {
//   //   path: "/main/analysis/dashboard",
//   //   component: () => import("@/views/main/analysis/dashboard/dashboard.vue")
//   // },
//   {
//     path: "/main/system/user",
//     component: () => import("@/views/main/system/user/user.vue")
//   },
//   {
//     path: "/main/system/role",
//     component: () => import("@/views/main/system/role/role.vue")
//   }
// ]

// // 动态添加路由
// router.addRoute('main', localRoutes[0])
// router.addRoute('main', localRoutes[1])


// 路由导航守卫,判断是否登录
router.beforeEach((to, from, next) => {
  const token = localCache.getCache(LOGIN_TOKEN)
  // !!TODO 这块逻辑有问题,暂时放开
  // if (to.path === "/main" && !token) {
  //   return '/login'
  // } else {
  //   next()
  // }
  // next()

  if (to.path.startsWith('/main') && !token) {
    return '/login'
  }
  //TODO !!这块逻辑有问题,目的就是要解决首次进行以后有数据,页面路由打开
  // 如果进入到main页面,
  // if (to.path === '/main') {
  //   return firstMenu?.url
  // }
  next()
})

export default router